import QtQuick 1.1
import Qt.labs.folderlistmodel 1.0

DialogLiquid {
    id: dlgFiles

    signal fileSelected( string file )
    signal fileCanceled()
    property string selectedFile: ""

    property string presetPath: ""

    Rectangle {
        id: background
        anchors.fill: parent
        color: "white"
    }

    height: 0.7*parent.height

    function stripFilePrefix(path) {
        var filePath = path.toString();
        return filePath.replace("file://", "")
    }

    function stripSuffix(path) {
        var filePath = path.toString()
        return filePath.substring(0, filePath.lastIndexOf("/"))
    }

    function setCurrentFolder(folder) {
        folderModel.folder = folder
    }

    Component.onCompleted: {
        if( presetPath != "" ) {
            setCurrentFolder( "file://" + presetPath )
        }
    }

    Component {
        id: delegateFolder
        Rectangle {
            id:wrapper
            height: 70
            width: list.width
            color: "transparent"
            radius: 10
            ListView.onAdd: SequentialAnimation {
                PauseAnimation { duration: 1+index>0 ? 40*index : 0 }
                NumberAnimation {
                    target: wrapper
                    property: "height"
                    from: 0; to: 70
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }
            Behavior on opacity { NumberAnimation{ duration: 300 } }
            Image {
                id: imgFolder
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 20
                source: folderModel.isFolder(index) ? "qrc:/qml/images/mime_folder.png" : "qrc:/qml/images/mime_wav.png"
            }
            Text {
                text: decodeURI(fileName)
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: imgFolder.right
                anchors.leftMargin: 20
                font.pixelSize: 28
                color: folderModel.isFolder(index) ? "black" : "blue"
            }

            MouseArea {
                id: maFolder
                anchors.fill: parent
                onClicked: {
                    if (folderModel.isFolder(index))
                    {
                        setCurrentFolder(filePath)
                    }
                    else
                    {
                        setCurrentFolder(folderModel.folder)
                        var file = stripFilePrefix(decodeURI(filePath))
                        console.log("Selected file: "+ file )
                        selectedFile = file
                        fileSelected( file )
                        dlgShowHide()
                        //pageImportWords.xmlfile = stripFilePrefix(decodeURI(filePath))
                    }
                }

                onExited: {
                    wrapper.color = "transparent"
                    wrapper.opacity = 1
                }

                onPressedChanged: {
                    if( pressed ) {
                        wrapper.color = "black"
                        wrapper.opacity = 0.5
                    } else {
                        wrapper.color = "transparent"
                        wrapper.opacity = 1
                    }
                }

            } //MouseArea

        }
    }


    Rectangle {
        id: header
        height: 30
        width: parent.width
        anchors.left: parent.left
        anchors.top: parent.top
        clip: true
        gradient: Gradient {
            GradientStop{ color: "lightgray"; position: 0 }
            GradientStop{ color: "gray"; position: 1 }
        }
        Image {
            id: imdFolder
            height: 26; width: height
            smooth: true
            source: "qrc:/qml/images/mime_folder.png"
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: 10
        }
        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: stripFilePrefix(folderModel.folder)
            font.pixelSize: 14
            anchors.left: imdFolder.right
            anchors.leftMargin: 10
        }
    }

    ListView {
        id: list
        clip: true
        anchors.top: header.bottom
        anchors.bottom: bottomPanel.top
        anchors.left: parent.left
        anchors.leftMargin: 5
        anchors.right: parent.right
        anchors.rightMargin: 5
        model: folderModel
        spacing: 2
        section.property: "path"
        delegate:  delegateFolder
    }

    FolderListModel {
        id: folderModel
        nameFilters: [ "*.wav", "*.WAV"/*, "*.ogg", "*OGG"*/ ]
        sortField: "Type"
    }

    ToolBar {
        id: bottomPanel
        ToolButton {
            id: backFolder
            anchors.left: parent.left
            anchors.leftMargin: 10
            icon: "qrc:/qml/images/bar_back.png"
            onClicked: {
                //console.log( "***|"+stripFilePrefix(folderModel.folder) )
                if (stripFilePrefix(folderModel.folder) !== "/") {
                    //setCurrentFolder(stripFilePrefix(stripSuffix(folderModel.folder)))
                    setCurrentFolder( stripSuffix(folderModel.folder) )
                }
            }
        }
        ToolButton {
            id: cancelDialog
            anchors.right: parent.right
            anchors.rightMargin : 10
            icon: "qrc:/qml/images/bar_close.png"
            onClicked: {
                fileCanceled()
                dlgShowHide()
            }
        }
    }

}
